from header_common import *
from header_operations import *
from header_parties import *
from header_items import *
from header_skills import *
from header_triggers import *
from header_troops import *
from header_colors import *

from module_constants import *

####################################################################################################################
#  Each trigger contains the following fields:
# 1) Check interval: How frequently this trigger will be checked
# 2) Delay interval: Time to wait before applying the consequences of the trigger
#    After its conditions have been evaluated as true.
# 3) Re-arm interval. How much time must pass after applying the consequences of the trigger for the trigger to become active again.
#    You can put the constant ti_once here to make sure that the trigger never becomes active again after it fires once.
# 4) Conditions block (list). This must be a valid operation block. See header_operations.py for reference.
#    Every time the trigger is checked, the conditions block will be executed.
#    If the conditions block returns true, the consequences block will be executed.
#    If the conditions block is empty, it is assumed that it always evaluates to true.
# 5) Consequences block (list). This must be a valid operation block. See header_operations.py for reference. 
####################################################################################################################


triggers = [
##########################################################################################
######################################################################## SYSTEMS TRIGGERS
##########################################################################################

# Harvest and calendar triggers
    (0.0, 24, 0.0, [(lt,"$day",31)],[(val_add,"$day",1)]),
    (0.1, 0, 0.0, [(eq,"$day",31),(lt,"$g_month",13)], [(val_add,"$g_month",1),(assign,"$day",1)]),
    (0.2, 0, 0.0, [(eq,"$g_month",13)],[(val_add,"$year",1),(assign,"$g_month",1)]),

    (0.0, 0, 25.0, [(eq,"$g_month",7),
                    (eq,"$day",1),
                    (eq,"$summer_harvest_active",0)],
     [(val_add,"$summer_harvest_active",1),
      (display_message,"@The summer harvest has begun.")]),
    (24.0, 0, 0.0, [(eq,"$g_month",8),
                    (eq,"$day",1),
                    (eq,"$summer_harvest_active",1)],
     [(val_sub,"$summer_harvest_active",1),
      (display_message,"@The summer harvest has ended.")]),
    
    (0.0, 0, 25.0, [(eq,"$g_month",10),
                    (eq,"$day",1),
                    (eq,"$autumn_harvest_active",0)],
     [(val_add,"$autumn_harvest_active",1),
      (display_message,"@The autumn harvest has begun.")]),
    (24.0, 0, 0.0, [(eq,"$g_month",11),
                    (eq,"$day",1),
                    (eq,"$autumn_harvest_active",1)],
     [(val_sub,"$autumn_harvest_active",1),
      (display_message,"@The autumn harvest has ended.")]),
    
    (0.0, 0, 25.0, [(eq,"$g_month",4),
                    (eq,"$day",1),
                    (eq,"$spring_harvest_active",0)],
     [(val_add,"$spring_harvest_active",1),
      (display_message,"@The spring harvest has begun.")]),
    (24.0, 0, 0.0, [(eq,"$g_month",5),
                    (eq,"$day",1),
                    (eq,"$spring_harvest_active",1)],
     [(val_sub,"$spring_harvest_active",1),
      (display_message,"@The spring harvest has ended.")]),

	  #Training triggers
    (0.0, 168, 0.0,
		[
			(check_quest_active,'qst_companion_training'),
			(neg|check_quest_succeeded,'qst_companion_training'),
			],
		[
			(quest_get_slot,':dojo','qst_companion_training',slot_quest_companion_training_dojo),
			(quest_get_slot,':sensei','qst_companion_training',slot_quest_companion_training_sensei),
			(quest_get_slot,':trainee','qst_companion_training',slot_quest_companion_training_companion),
			(scene_set_slot,':dojo',slot_dojo_training,0),
			(scene_set_slot,':dojo',slot_dojo_sensei_away,0),
			(try_begin),
				(store_skill_level,':trainee_skill_level',skl_power_strike,':trainee'),
				(troop_get_slot,':sensei_skill_bonus',':sensei',slot_sensei_skl_ps),
				(val_add,':trainee_skill_level',':sensei_skill_bonus'),
				(le,':trainee_skill_level',10),
				(troop_raise_skill,':trainee',skl_power_strike,':sensei_skill_bonus'),
			(else_try),
				(store_skill_level,':trainee_skill_level',skl_power_draw,':trainee'),
				(troop_get_slot,':sensei_skill_bonus',':sensei',slot_sensei_skl_ps),
				(val_add,':trainee_skill_level',':sensei_skill_bonus'),
				(le,':trainee_skill_level',10),
				(troop_raise_skill,':trainee',skl_power_draw,':sensei_skill_bonus'),
			(else_try),
				(store_skill_level,':trainee_skill_level',skl_power_throw,':trainee'),
				(troop_get_slot,':sensei_skill_bonus',':sensei',slot_sensei_skl_ps),
				(val_add,':trainee_skill_level',':sensei_skill_bonus'),
				(le,':trainee_skill_level',10),
				(troop_raise_skill,':trainee',skl_power_throw,':sensei_skill_bonus'),
			(else_try),
				(store_skill_level,':trainee_skill_level',skl_weapon_master,':trainee'),
				(troop_get_slot,':sensei_skill_bonus',':sensei',slot_sensei_skl_ps),
				(val_add,':trainee_skill_level',':sensei_skill_bonus'),
				(le,':trainee_skill_level',10),
				(troop_raise_skill,':trainee',skl_weapon_master,':sensei_skill_bonus'),
			(else_try),
				(store_skill_level,':trainee_skill_level',skl_athletics,':trainee'),
				(troop_get_slot,':sensei_skill_bonus',':sensei',slot_sensei_skl_ps),
				(val_add,':trainee_skill_level',':sensei_skill_bonus'),
				(le,':trainee_skill_level',10),
				(troop_raise_skill,':trainee',skl_athletics,':sensei_skill_bonus'),
			(try_end),
			(troop_get_slot,':sensei_proficiency_bonus',':sensei',slot_sensei_prof_oh),
			(troop_raise_proficiency_linear,':trainee',0,':sensei_proficiency_bonus'),
			(troop_get_slot,':sensei_proficiency_bonus',':sensei',slot_sensei_prof_th),
			(troop_raise_proficiency_linear,':trainee',1,':sensei_proficiency_bonus'),
			(troop_get_slot,':sensei_proficiency_bonus',':sensei',slot_sensei_prof_pl),
			(troop_raise_proficiency_linear,':trainee',2,':sensei_proficiency_bonus'),
			(troop_get_slot,':sensei_proficiency_bonus',':sensei',slot_sensei_prof_ar),
			(troop_raise_proficiency_linear,':trainee',3,':sensei_proficiency_bonus'),
			(troop_get_slot,':sensei_proficiency_bonus',':sensei',slot_sensei_prof_th),
			(troop_raise_proficiency_linear,':trainee',5,':sensei_proficiency_bonus'),
			(call_script,'script_succeed_quest','qst_companion_training'),
			(display_message,"str_training_complete"),
			]
		),

# Wages and faction points
(168.0, 0, 0.0,
	[
		(this_or_next|faction_slot_eq,'fac_hosokawa',slot_faction_joined,true),
		(faction_slot_eq,'fac_yamana',slot_faction_joined,true),
		],
	[
		(troop_get_slot,':rank_array','trp_player',slot_player_rank_array),
		(troop_get_slot,':rank_array_ptr','trp_player',slot_player_rank_ptr),
		(call_script,'script_get_rank_pay_and_fp_rate',':rank_array',':rank_array_ptr'),
		(assign,':pay_rate',reg0),
		(assign,':fp_rate',reg1),
		(call_script,'script_get_rank_wages',':rank_array',':rank_array_ptr'),
		(assign,':wages',reg0),
		(call_script,'script_get_rank_fp',':rank_array',':rank_array_ptr'),
		(assign,':fp',reg0),
		(val_add,":pay_rate",":wages"),
		(val_add,":fp_rate",":fp"),
		(call_script,'script_set_rank_wages',':rank_array',':rank_array_ptr',':wages'),
		(call_script,'script_set_rank_fp',':rank_array',':rank_array_ptr',':fp'),
		]),
		 

##############################################################################################################
############################################################################################ SPAWING TRIGGERS
##############################################################################################################

# test shoen trigger
	(1.0, 0, ti_once, [],
		[
			(assign,':town',itami),
			(party_get_slot,':capital',':town',slot_town_capital),
			(party_get_slot,':broker',':capital',slot_town_broker),
			(call_script,'script_initialize_shoen',':town',':broker'),
			(str_store_party_name,10,':town'),
			(str_store_party_name,11,':capital'),
			(str_store_troop_name,12,':broker'),
			(display_message,'@The land around {s10} has been consolidated into a shoen. See {s12} in {s11} for more information.',green_text),
			]
		),
#Farm to Market spawning
(4.0, 0, 72.0, [],
	[
		(try_for_range,':town',villages_begin,villages_end),
			(party_slot_neq,':town',slot_town_under_siege,1),
			(party_slot_eq,':town',slot_town_trade_hub,0),
			(party_slot_eq,':town',slot_town_trade_month,'$g_month'),
			(party_get_slot,':town_size',':town',slot_town_size),
			(store_mul,':min_rice_stores',':town_size',7),
			(party_slot_ge,':town',slot_town_food_stores,':min_rice_stores'),
			(party_slot_neq,':town',slot_town_trader_active,1),
			(call_script,'script_get_trade_dest',':town'),
			(assign,':td_obj',reg0),
			(try_begin),
				# (eq,'$g_debug_msg_on',true),
				(eq,':td_obj',null),
				(str_store_party_name,10,':town'),
				(display_message,'@{s10} has a null reference for td_obj',debug_text),
			(try_end),
			(party_get_slot,':origin',':td_obj',slot_td_origin),
			(party_get_slot,':dest',':td_obj',slot_td_dest),
			(set_spawn_radius,0),
			(spawn_around_party,':town',"pt_traveling_farmers"),
			(assign,':traveling_farmer',reg0),
			(party_set_ai_behavior,':traveling_farmer',ai_bhvr_travel_to_party),
			(party_set_ai_object,':traveling_farmer',':dest'),
			(party_set_slot,':traveling_farmer',slot_spawn_att_to_player,att_friendly),
			(party_set_slot,':traveling_farmer',slot_spawn_origin,':origin'),
			(party_set_slot,':traveling_farmer',slot_spawn_dest,':dest'),
			(party_set_slot,':traveling_farmer',slot_spawn_gold,0),
			(party_set_slot,':traveling_farmer',slot_spawn_type,spt_village_farmer),
			(party_set_slot,':traveling_farmer',slot_spawn_ai_state,spai_trading_with_town),
			(party_set_slot,':traveling_farmer',slot_spawn_ai_object,':dest'),
			(party_set_slot,':traveling_farmer',slot_spawn_icon,'icon_mule'),
			(party_set_slot,':traveling_farmer',slot_spawn_td_obj,':td_obj'),
			(call_script,'script_add_rice_to_travelling_merchant',':town',':traveling_farmer',spt_village_farmer),
			(party_set_slot,':town',slot_town_trader,':traveling_farmer'),
			(party_set_slot,':town',slot_town_trader_active,1),
			(val_add,'$g_num_active_traveling_farmers',1),
		(try_end),
		]
	),
    
#Merchant caravan spawning
    #all hubs
(3.0, 0, 72.0, [],
	[
		#(display_message,'@Spawn city merchants'),
		(try_for_range,':town',hyogo,cities_end),
			(party_slot_neq,':town',slot_town_trader_active,1),
			(party_slot_eq,':town',slot_town_trade_hub,1),
			(call_script,'script_get_trade_dest',':town'),
			(assign,':td_obj',reg0),
			(party_get_slot,':origin',':td_obj',slot_td_origin),
			(party_get_slot,':dest',':td_obj',slot_td_dest),
			(try_begin),
				# (eq,'$g_debug_msg_on',true),
				(eq,':td_obj',null),
				(str_store_party_name,10,':town'),
				(display_message,'@{s10} has a null reference for td_obj',debug_text),
			(try_end),
			(set_spawn_radius,0),
			(spawn_around_party,':origin','pt_traveling_peddler'),
			(assign,':traveling_merchant',reg0),
			(party_set_slot,':traveling_merchant',slot_spawn_origin,':town'),
			(party_set_ai_behavior,':traveling_merchant',ai_bhvr_travel_to_party),
			(party_set_ai_object,':traveling_merchant',':dest'),
			(party_set_slot,':origin',slot_town_trader,':traveling_merchant'),
			(party_set_slot,':origin',slot_town_trader_active,1),
			(party_set_slot,':traveling_merchant',slot_spawn_dest,':dest'),
			(party_set_slot,':traveling_merchant',slot_spawn_att_to_player,att_cautious),
			(party_set_slot,':traveling_merchant',slot_spawn_origin,':origin'),
			(party_set_slot,':traveling_merchant',slot_spawn_gold,0),
			(party_set_slot,':traveling_merchant',slot_spawn_type,spt_merchant),
			(party_set_slot,':traveling_merchant',slot_spawn_ai_state,spai_trading_with_town),
			(party_set_slot,':traveling_merchant',slot_spawn_ai_object,':dest'),
			(party_set_slot,':traveling_merchant',slot_spawn_icon,'icon_mule'),
			(party_set_slot,':traveling_merchant',slot_spawn_td_obj,':td_obj'),
			(call_script,'script_add_rice_to_travelling_merchant',':origin',':traveling_merchant',spt_merchant),
			(val_add,'$g_num_active_traveling_merchants',1),
		(try_end),
		]
	),
(4.0, 0, 96.0, [],
	[
		#(display_message,'@Spawn town merchants'),
		(try_for_range,':town',towns_begin,towns_end),
			(party_slot_neq,':town',slot_town_trader_active,1),
			(party_slot_eq,':town',slot_town_trade_hub,1),
			(call_script,'script_get_trade_dest',':town'),
			(assign,':td_obj',reg0),
			(try_begin),
				# (eq,'$g_debug_msg_on',true),
				(eq,':td_obj',null),
				(str_store_party_name,10,':town'),
				(display_message,'@{s10} has a null reference for td_obj',debug_text),
			(try_end),
			(party_get_slot,':origin',':td_obj',slot_td_origin),
			(party_get_slot,':dest',':td_obj',slot_td_dest),
			(set_spawn_radius,0),
			(spawn_around_party,':town','pt_traveling_peddler'),
			(assign,':traveling_merchant',reg0),
			(party_set_slot,':traveling_merchant',slot_spawn_origin,':town'),
			(party_set_ai_behavior,':traveling_merchant',ai_bhvr_travel_to_party),
			(party_set_ai_object,':traveling_merchant',':dest'),
			(party_set_slot,':town',slot_town_trader,':traveling_merchant'),
			(party_set_slot,':town',slot_town_trader_active,true),
			(party_set_slot,':traveling_merchant',slot_spawn_dest,':dest'),
			(party_set_slot,':traveling_merchant',slot_spawn_att_to_player,att_cautious),
			(party_set_slot,':traveling_merchant',slot_spawn_origin,':origin'),
			(party_set_slot,':traveling_merchant',slot_spawn_gold,0),
			(party_set_slot,':traveling_merchant',slot_spawn_type,spt_merchant),
			(party_set_slot,':traveling_merchant',slot_spawn_ai_state,spai_trading_with_town),
			(party_set_slot,':traveling_merchant',slot_spawn_ai_object,':dest'),
			(party_set_slot,':traveling_merchant',slot_spawn_icon,'icon_mule'),
			(party_set_slot,':traveling_merchant',slot_spawn_td_obj,':td_obj'),
			(call_script,'script_add_rice_to_travelling_merchant',':origin',':traveling_merchant',spt_merchant),
			(val_add,'$g_num_active_traveling_merchants',1),
		(try_end),
		]
	),

#Misc Spawns
    (24.0, 0, 0.0, [],
     [
         (store_random_in_range,':origin',all_towns_begin,all_towns_end),
         (store_random_in_range,':dest',all_towns_begin,all_towns_end),
         (neq,':origin',':dest'),
         (set_spawn_radius,0),
		 (spawn_around_party,':origin',"pt_travellers"),
		 (party_set_slot,reg0,slot_spawn_att_to_player,att_friendly),
		 (party_set_ai_behavior,reg0,ai_bhvr_travel_to_party),
		 (party_set_ai_object,reg0,':dest'),
		 (call_script,'script_object_modify_slot',obj_party,reg0,slot_party_type,op_asn,spt_traveler),
         ]),
    (72.0, 0, 0.0, [],
     [
         (store_random_in_range,':origin',all_towns_begin,all_towns_end),
         (set_spawn_radius,0),
         (spawn_around_party,':origin',"pt_pilgrims"),
         (party_set_slot,reg0,slot_spawn_att_to_player,att_friendly),
         (party_set_ai_behavior,reg0,ai_bhvr_travel_to_party),
         (party_set_ai_object,reg0,"p_temple_1"),
		 (call_script,'script_object_modify_slot',obj_party,reg0,slot_party_type,op_asn,spt_traveler),
		 ]),
    ((24.0*7), 0, 0.0, [],
     [
         (store_random_in_range,':origin',all_towns_begin,all_towns_end),
         (set_spawn_radius,0),
         (spawn_around_party,':origin',"pt_monk_pilgrim"),
         (party_set_slot,reg0,slot_spawn_att_to_player,att_friendly),
         (party_set_ai_behavior,reg0,ai_bhvr_travel_to_party),
         (party_set_ai_object,reg0,"p_temple_1"),
		 (call_script,'script_object_modify_slot',obj_party,reg0,slot_party_type,op_asn,spt_traveler),
		 ]),
    ((24.0*10), 0, 0.0, [],
     [
         (set_spawn_radius,0),
         (spawn_around_party,"p_mountain_bandit_spawn_point","pt_yamabushi"),
         (party_set_slot,reg0,slot_spawn_att_to_player,att_friendly),
		 (call_script,'script_object_modify_slot',obj_party,reg0,slot_party_type,op_asn,spt_traveler),
		 ]),
    ((24.0*3), 0, 0.0, [],
     [
         (store_random_in_range,':dest',hyogo,akashi),
         (store_random_in_range,':origin',south_kyoto,hyogo),
         (set_spawn_radius,1),
         (spawn_around_party,':origin',"pt_refugees"),
         (party_set_slot,reg0,slot_spawn_att_to_player,att_friendly),
         (party_set_ai_behavior,reg0,ai_bhvr_travel_to_party),
         (party_set_ai_object,reg0,':dest'),
		 (call_script,'script_object_modify_slot',obj_party,reg0,slot_party_type,op_asn,spt_traveler),
		 ]),
    (24.0, 0, 0.0,
		[
			(store_num_parties_of_template,reg1,'pt_duelist'),
			(le,reg1,0),
			],
		[
			(eq,1,0),
			(store_random_in_range,reg1,hyogo,cities_end),
			(set_spawn_radius,0),
			(spawn_around_party,reg1,"pt_duelist"),
			(party_set_slot,reg0,slot_spawn_att_to_player,att_secretive),
			(call_script,'script_object_modify_slot',obj_party,reg0,slot_party_type,op_asn,spt_traveler),
			]),
##############################################################################################################
########################################################################################### BEHAVIOR TRIGGERS
##############################################################################################################

#Return farmers to home towns
(0.0, 3, 0.0, 
	[
		(gt,'$g_num_active_traveling_farmers',0),
		],
	[
		(try_for_parties,':farmers'),
			(call_script,'script_cf_party_is_of_template',':farmers','pt_traveling_farmers'),
			(party_get_slot,':dest',':farmers',slot_spawn_dest),
			(party_get_slot,':origin',':farmers',slot_spawn_origin),
			(party_is_in_town,':farmers',':dest'),
			(call_script,'script_get_trade_revenue',':farmers',':dest'),
			(party_set_ai_behavior,':farmers',ai_bhvr_travel_to_party),
			(party_set_ai_object,':farmers',':origin'),
			(party_set_slot,':farmers',slot_spawn_ai_state,spai_trading_with_town),
			(party_set_slot,':farmers',slot_spawn_ai_object,':origin'),
		(try_end),
        ]),
    
(0.0, 0, 0.5,
	[
		(gt,'$g_num_active_traveling_farmers',0),
		],
	[
		(try_for_parties,':farmers'),
			(call_script,'script_cf_party_is_of_template',':farmers','pt_traveling_farmers'),
			(party_get_slot,':td_obj',':farmers',slot_spawn_td_obj),
			(party_get_slot,':origin',':td_obj',slot_td_origin),
			(party_is_in_town,':farmers',':origin'),
			(call_script,'script_generate_trade_revenue',':origin',':farmers'),
			(party_set_slot,':origin',slot_town_trader_active,false),
			(party_set_slot,':origin',slot_town_trader,null),
			(remove_party,':farmers'),
			(val_sub,'$g_num_active_traveling_farmers',1),
		(try_end),
		]
	),

    # Get the merchants to their next destination
(0.0, 12, 0.0,[(gt,'$g_num_active_traveling_merchants',0)],
	[
		(try_for_parties,':traveling_merchant'),
			(call_script,'script_cf_party_is_of_template',':traveling_merchant','pt_traveling_peddler'),
			(party_get_slot,':td_obj',':traveling_merchant',slot_spawn_td_obj),
			(party_get_slot,':origin',':td_obj',slot_td_origin),
			(party_get_slot,':dest',':td_obj',slot_td_dest),
			(party_is_in_town,':traveling_merchant',':dest'),
			(call_script,'script_get_trade_revenue',':traveling_merchant',':dest'),
			(party_set_ai_behavior,':traveling_merchant',ai_bhvr_travel_to_party),
			(party_set_ai_object,':traveling_merchant',':origin'),
			(party_set_slot,':traveling_merchant',slot_spawn_ai_state,spai_trading_with_town),
			(party_set_slot,':traveling_merchant',slot_spawn_ai_object,':origin'),
		(try_end),
		]
	),
(0.0, 0, 0.5,
	[(gt,'$g_num_active_traveling_merchants',0)],
	[
		(try_for_parties,':traveling_merchant'),
			(call_script,'script_cf_party_is_of_template',':traveling_merchant','pt_traveling_peddler'),
			(party_get_slot,':td_obj',':traveling_merchant',slot_spawn_td_obj),
			(party_get_slot,':origin',':td_obj',slot_td_origin),
			(party_is_in_town,':traveling_merchant',':origin'),
			(call_script,'script_generate_trade_revenue',':origin',':traveling_merchant'),
			(party_set_slot,':origin',slot_town_trader_active,false),
			(party_set_slot,':origin',slot_town_trader,null),
			(remove_party,':traveling_merchant'),
			(val_sub,'$g_num_active_traveling_merchants',1),
		(try_end),
		]),

 

##############################################################################################################
############################################################################################ SPAWING TRIGGERS
##############################################################################################################

# Hosokawa spawning
(12.0, 0, 0.0, 
	[
		(store_num_parties_of_template,reg2,"pt_hosokawa_patrol"),
		(lt,reg2,"$max_hosokawa_patrols"),
		],
	[
		(call_script,'script_cf_faction_spawn_patrol','fac_hosokawa','pt_hosokawa_patrol'),
		]
	),
    (8.0, 0, 0.0, [(store_num_parties_of_template,reg2,"pt_hosokawa_messenger"),
                   (lt,reg2,"$max_hosokawa_messengers")],
     [
         (call_script,"script_select_random_faction_city",'fac_hosokawa'),
         (assign,':origin','$return'),
         (call_script,"script_select_random_faction_town",'fac_hosokawa'),
         (assign,':dest','$return'),
         (set_spawn_radius,0),
         (spawn_around_party,':origin',"pt_hosokawa_messenger"),
         (party_set_ai_behavior,reg0,ai_bhvr_travel_to_party),
         (party_set_ai_object,reg0,':dest'),
         (party_set_slot,reg0,slot_spawn_type,spt_messenger),
		 (party_set_slot,reg0,slot_spawn_ai_state,spai_traveling_to_center),
		 (party_set_slot,reg0,slot_spawn_ai_object,':dest'),
         (faction_get_slot,reg1,'fac_hosokawa',slot_faction_att_to_player),
         (party_set_slot,reg0,slot_spawn_att_to_player,reg1),
		 (call_script,'script_object_modify_slot',obj_party,reg0,slot_party_type,op_asn,spt_scout),
         ]
     ),
    (8.0, 0, 0.0, [(store_num_parties_of_template,reg2,"pt_hosokawa_messenger"),
                   (lt,reg2,"$max_hosokawa_messengers")],
     [
         (call_script,"script_select_random_faction_town",'fac_hosokawa'),
         (assign,':origin','$return'),
         (call_script,"script_select_random_faction_city",'fac_hosokawa'),
         (assign,':dest','$return'),
         (set_spawn_radius,0),
         (spawn_around_party,':origin',"pt_hosokawa_messenger"),
         (party_set_ai_behavior,reg0,ai_bhvr_travel_to_party),
         (party_set_ai_object,reg0,':dest'),
         (party_set_slot,reg0,slot_spawn_type,spt_messenger),
		 (party_set_slot,reg0,slot_spawn_ai_state,spai_traveling_to_center),
		 (party_set_slot,reg0,slot_spawn_ai_object,':dest'),
         (faction_get_slot,reg1,'fac_hosokawa',slot_faction_att_to_player),
         (party_set_slot,reg0,slot_spawn_att_to_player,reg1),
		 (call_script,'script_object_modify_slot',obj_party,reg0,slot_party_type,op_asn,spt_scout),
         ]
     ),
    (12.0, 0, 0.0, [(store_num_parties_of_template,reg2,"pt_hosokawa_scouts"),
                    (lt,reg2,"$max_hosokawa_scouts")],
     [
         (call_script,"script_select_random_faction_town",'fac_hosokawa'),
         (assign,':town','$return'),
         (set_spawn_radius,1),
         (spawn_around_party,':town',"pt_hosokawa_scouts"),
         (party_set_slot,reg0,slot_spawn_type,spt_patrol),
		 (party_set_slot,reg0,slot_spawn_ai_state,spai_patroling),
		 (party_set_slot,reg0,slot_spawn_ai_object,null),
         (faction_get_slot,reg1,'fac_hosokawa',slot_faction_att_to_player),
         (party_set_slot,reg0,slot_spawn_att_to_player,reg1),
		 (call_script,'script_object_modify_slot',obj_party,reg0,slot_party_type,op_asn,spt_scout),
         ]
     ),
    (12.0, 0, 0.0, [(store_num_parties_of_template,reg2,"pt_hosokawa_supply_train"),
                    (lt,reg2,"$max_hosokawa_supply_trains")],
     [
         (call_script,"script_select_random_faction_city",'fac_hosokawa'),
         (assign,':origin','$return'),
         (call_script,"script_select_random_faction_city",'fac_hosokawa'),
         (assign,':dest','$return'),
         (set_spawn_radius,0),
         (spawn_around_party,':origin',"pt_hosokawa_supply_train"),
         (party_set_ai_behavior,reg0,ai_bhvr_travel_to_party),
         (party_set_ai_object,reg0,':dest'),
         (party_set_slot,reg0,slot_spawn_type,spt_kingdom_caravan),
		 (party_set_slot,reg0,slot_spawn_ai_state,spai_traveling_to_center),
		 (party_set_slot,reg0,slot_spawn_ai_object,':dest'),
         (faction_get_slot,reg1,'fac_hosokawa',slot_faction_att_to_player),
         (party_set_slot,reg0,slot_spawn_att_to_player,reg1),
		 (call_script,'script_object_modify_slot',obj_party,reg0,slot_party_type,op_asn,spt_kingdom_caravan),
         ]
     ),
# Yamana spawning
(12.0, 0, 0.0,
	[
		(store_num_parties_of_template,':num_patrols',"pt_yamana_patrol"),
		(lt,':num_patrols',"$max_yamana_patrols"),
		],
	[
		(call_script,'script_cf_faction_spawn_patrol','fac_yamana','pt_yamana_patrol'),
		]
	),
    (8.0, 0, 0.0, [(store_num_parties_of_template,reg2,"pt_yamana_messenger"),
                   (lt,reg2,"$max_yamana_messengers")],
     [
         (call_script,"script_select_random_faction_city",'fac_yamana'),
         (assign,':origin','$return'),
         (call_script,"script_select_random_faction_town",'fac_yamana'),
         (assign,':dest','$return'),
         (set_spawn_radius,0),
         (spawn_around_party,':origin',"pt_yamana_messenger"),
         (party_set_ai_behavior,reg0,ai_bhvr_travel_to_party),
         (party_set_ai_object,reg0,':dest'),
         (party_set_slot,reg0,slot_spawn_type,spt_messenger),
		 (party_set_slot,reg0,slot_spawn_ai_state,spai_traveling_to_center),
		 (party_set_slot,reg0,slot_spawn_ai_object,':dest'),
         (faction_get_slot,reg1,'fac_yamana',slot_faction_att_to_player),
         (party_set_slot,reg0,slot_spawn_att_to_player,reg1),
		 (call_script,'script_object_modify_slot',obj_party,reg0,slot_party_type,op_asn,spt_scout),
         ]
     ),
    (8.0, 0, 0.0, [(store_num_parties_of_template,reg2,"pt_yamana_messenger"),
                   (lt,reg2,"$max_yamana_messengers")],
     [
         (call_script,"script_select_random_faction_town",'fac_yamana'),
         (assign,':origin','$return'),
         (call_script,"script_select_random_faction_city",'fac_yamana'),
         (assign,':dest','$return'),
         (set_spawn_radius,0),
         (spawn_around_party,':origin',"pt_yamana_messenger"),
         (party_set_ai_behavior,reg0,ai_bhvr_travel_to_party),
         (party_set_ai_object,reg0,':dest'),
         (party_set_slot,reg0,slot_spawn_type,spt_messenger),
		 (party_set_slot,reg0,slot_spawn_ai_state,spai_traveling_to_center),
		 (party_set_slot,reg0,slot_spawn_ai_object,':dest'),
         (faction_get_slot,reg1,'fac_yamana',slot_faction_att_to_player),
         (party_set_slot,reg0,slot_spawn_att_to_player,reg1),
		 (call_script,'script_object_modify_slot',obj_party,reg0,slot_party_type,op_asn,spt_scout),
         ]
     ),
    (12.0, 0, 0.0, [(store_num_parties_of_template,reg2,"pt_yamana_scouts"),
                    (lt,reg2,"$max_yamana_scouts")],
     [
         (call_script,"script_select_random_faction_town","fac_yamana"),
         (assign,':town','$return'),
         (set_spawn_radius,1),
         (spawn_around_party,':town',"pt_yamana_scouts"),
         (party_set_slot,reg0,slot_spawn_type,spt_patrol),
		 (party_set_slot,reg0,slot_spawn_ai_state,spai_patroling),
		 (party_set_slot,reg0,slot_spawn_ai_object,null),
         (faction_get_slot,reg1,'fac_yamana',slot_faction_att_to_player),
         (party_set_slot,reg0,slot_spawn_att_to_player,reg1),
		 (call_script,'script_object_modify_slot',obj_party,reg0,slot_party_type,op_asn,spt_scout),
         ]
     ),
    (12.0, 0, 0.0, [(store_num_parties_of_template,reg2,"pt_yamana_supply_train"),
                    (lt,reg2,"$max_yamana_supply_trains")],
     [
         (call_script,"script_select_random_faction_city",'fac_yamana'),
         (assign,':origin','$return'),
         (call_script,"script_select_random_faction_city",'fac_yamana'),
         (assign,':dest','$return'),
         (set_spawn_radius,0),
         (spawn_around_party,':origin',"pt_yamana_supply_train"),
         (party_set_ai_behavior,reg0,ai_bhvr_travel_to_party),
         (party_set_ai_object,reg0,':dest'),
         (party_set_slot,reg0,slot_spawn_type,spt_kingdom_caravan),
		 (party_set_slot,reg0,slot_spawn_ai_state,spai_traveling_to_center),
		 (party_set_slot,reg0,slot_spawn_ai_state,spai_traveling_to_center),
		 (party_set_slot,reg0,slot_spawn_ai_object,':dest'),
         (faction_get_slot,reg1,'fac_yamana',slot_faction_att_to_player),
         (party_set_slot,reg0,slot_spawn_att_to_player,reg1),
		 (call_script,'script_object_modify_slot',obj_party,reg0,slot_party_type,op_asn,spt_kingdom_caravan),
         ]
     ),

    (0.0, 0, 0.0, [(key_clicked,key_k)],
		[
			(party_get_position,pos10,'p_main_party'),
			(position_get_x,reg10,pos10),
			(position_get_y,reg11,pos10),
			(display_message,'@X: {reg10}, Y: {reg11}',debug_text),
			]),
	(0.0, 0, 0.0, [(key_clicked,key_m)],
		[
			(display_message,"@key 'm' clicked",debug_text),
			# (store_num_parties_of_template,':num_patrols','pt_local_patrol'),
			# (le,':num_patrols','$max_local_patrols'),
			# (try_for_range,':town',villages_begin,villages_end),
				# (party_slot_neq,':town',slot_town_patrol_active,1),
				(party_set_slot,itami,slot_town_patrol_active,0),
				(call_script,'script_object_modify_slot',obj_party,itami,slot_town_spawn_points,op_add,10000),
				(call_script,'script_cf_spawn_local_patrol',itami,'pt_village_patrol'),
			# (try_end),
			]
		),
	
	
##############################################################################################################
############################################################################################## QUEST TRIGGERS
##############################################################################################################

# repair weapons quest
(0.0, 24, 0.0, [(check_quest_active,'qst_repair_weapon'),(neg|check_quest_concluded,'qst_repair_weapon')],
	[
		(quest_set_slot,'qst_repair_weapon',slot_quest_item_1_imod,imod_plain),
		(call_script,'script_conclude_quest','qst_repair_weapon'),
		(display_message,'@Your item has been repaired',green_text),
		]
	),
# Job board shugo quests
# Scout
(1.0, 0, 0.0, 
	[
		(check_quest_active,'qst_jb_faction_scout'),
		(neg|check_quest_succeeded,'qst_jb_faction_scout'),
		],
	[
		(assign,':break',false),
		(quest_get_slot,':target_template','qst_jb_faction_scout',slot_quest_target_party_template),
		(party_get_skill_level,':party_pathfinding_skill','p_main_party',skl_pathfinding),
		(try_begin),
			(is_currently_night),
			(store_add,':max_detection_distance',':party_pathfinding_skill',5),
		(else_try),
			(store_add,':max_detection_distance',':party_pathfinding_skill',5),
		(try_end),
		(try_for_parties,':party_no'),
			(eq,':break',false),
			(party_get_template_id,':template',':party_no'),
			(eq,':template',':target_template'),
			(store_distance_to_party_from_party,':dist','p_main_party',':party_no'),
			(le,':dist',':max_detection_distance'),
			(quest_set_slot,'qst_jb_faction_scout',slot_quest_target_party,':party_no'),
			(assign,':break',true),
			(call_script,'script_succeed_quest','qst_jb_faction_scout'),
		(try_end),
		],
	),
# Kokujin bandits quest
(0.1, 0, 0.0, 
	[
		(check_quest_active,'qst_kokujin_defeat_bandits'),
		(neg|check_quest_succeeded,'qst_kokujin_defeat_bandits'),
		],
	[
		(quest_get_slot,':target_party','qst_kokujin_defeat_bandits',slot_quest_target_party),
		(neg|party_is_active,':target_party'),
		(call_script,'script_succeed_quest','qst_kokujin_defeat_bandits'),
		]
	),
#Sanda bandits quest
    (0.0, 0, 0.0,
		[
			(check_quest_active,'qst_sanda_bandits'),
			(neg|check_quest_concluded,'qst_sanda_bandits'),
			],
		[
			(store_num_parties_destroyed_by_player,reg3,"pt_bandits"),
			(val_sub,reg3,'$g_num_bandits_defeated'),
			(assign,reg2,100),
			(val_mul,reg2,reg3),
			(val_div,reg2,10),
			(set_quest_progression,"qst_sanda_bandits",reg2)]),
			
    (0.1, 0, ti_once,
		[
			(check_quest_active,'qst_sanda_bandits'),
			(neg|check_quest_succeeded,'qst_sanda_bandits'),
			(store_num_parties_destroyed_by_player,reg1,"pt_bandits"),
			(val_sub,reg1,'$g_num_bandits_defeated'),
			(ge,reg1,10),
			],
		[
			(set_quest_progression,"qst_sanda_bandits",100),
			(call_script,'script_succeed_quest','qst_sanda_bandits'),
			(val_sub,"$max_bandits",7),
			(display_message,"str_sanda_quest_complete")]),
			
#Intro defeat pirates quest
    (0.0, 0, 0.0,
		[
			(check_quest_active,'qst_defeat_pirates'),
			(neg|check_quest_succeeded,'qst_defeat_pirates'),
			],
		[
			(store_num_parties_destroyed_by_player,reg3,"pt_river_pirates"),
			(val_sub,reg3,'$num_river_pirates'),
			(assign,reg2,100),
			(val_mul,reg2,reg3),
			(val_div,reg2,10),
			(set_quest_progression,"qst_defeat_pirates",reg2),
			]
		),
    (0.1, 0, ti_once, [(check_quest_active,'qst_defeat_pirates'),
					   (neg|check_quest_succeeded,'qst_defeat_pirates'),
                       (store_num_parties_destroyed_by_player,reg1,"pt_river_pirates"),
                       (val_sub,reg1,'$num_river_pirates'),
                       (ge,reg1,10)],
     [(set_quest_progression,"qst_defeat_pirates",100),
      (call_script,'script_succeed_quest',"qst_defeat_pirates"),
      (val_sub,"$max_river_pirates",9),
      (display_message,"str_river_pirates_complete")]),
(24.0, 0, ti_once, [(neg|check_quest_active,'qst_subclass_kengo')],
	[
		(troop_get_slot,':training_flags','trp_player',slot_player_training_flags),
		(eq,':training_flags',255),
		(start_quest,'qst_subclass_kengo'),
		(dialog_box,'str_kengo_achieved','@You are kengo'),
		]),
#Intro kuge quest
    (0.0, 2, ti_once, [(eq,"$intro_kuge_quest_given",1)],
     [
         (set_spawn_radius,10),
         (spawn_around_party,"p_main_party","pt_bandit_raiders_a"),
         (assign,"$intro_kuge_bandits",reg0),
         (party_set_ai_behavior,reg0,ai_bhvr_attack_party),
         (party_set_ai_object,reg0,"p_main_party"),
         (party_add_members,reg0,"trp_bandit_warlord",1),
         (store_party_size_wo_prisoners,reg1,"p_main_party"),
         (store_party_size_wo_prisoners,reg2,"$intro_kuge_bandits"),
         (try_begin),
             (lt,reg2,reg1),
             (assign,reg3,reg1),
             (val_sub,reg3,reg2),
             (val_add,reg3,2),
             (party_add_members,reg0,"trp_bandit",reg3),
         (try_end)
         ]
     ),
	(0.0, 0, ti_once, [(check_quest_finished,"qst_intro_kuge_deliver_msg")],
		[
			(remove_party,"$intro_kuge_bandits")]),
#Bladesmith quest
    (0.0, 0, 24, [(check_quest_active,"qst_bladesmith_2"),
                  (store_current_day,reg63),
                  (lt,reg63,"$bladesmith_end_day")],
     [
         (assign,reg2,"$bladesmith_end_day"),
         (store_current_day,reg3),
         (val_sub,reg2,reg3),
         (assign,reg1,reg2),
         (setup_quest_text,"qst_bladesmith_2")]),
    
    (0.0, 0, ti_once, [(check_quest_active,"qst_bladesmith_2"),
                       (store_current_day,reg63),
                       (eq,reg63,"$bladesmith_end_day")],
     [
         (troop_set_slot,'trp_town_5_weaponsmith',50,1),
         (display_message,"str_bladesmith_finished")]),
# Random bushi quest triggers
# Reconnaisance quest
(0.0, 0, 0.0, 
	[
		(check_quest_active,"qst_bushi_reconnoiter_enemy_army"),
		(neg|check_quest_succeeded,"qst_bushi_reconnoiter_enemy_army"),
		(quest_get_slot,':target_party','qst_bushi_reconnoiter_enemy_army',slot_quest_target_party),
		(store_distance_to_party_from_party,':distance_to_target_party',"p_main_party",':target_party'),
		(le,':distance_to_target_party',"$g_scout_range"),
		],
	[
		(display_message,"str_shugo_reconnaisance_complete"),
		(call_script,'script_succeed_quest','qst_bushi_reconnoiter_enemy_army'),
		],
	),
# Rebels quest
(0.0, 0, 0.5,
	[
		(check_quest_active,"qst_bushi_suppress_rebels"),
		(neg|check_quest_succeeded,"qst_bushi_reconnoiter_enemy_army"),
		(quest_get_slot,':target_rebel_party','qst_bushi_suppress_rebels',slot_quest_target_party_2),
		(neg|party_is_active,':target_rebel_party'),
		(eq,'$shugo_rebel_flag',1),
		],
	[
		(display_message,"str_shugo_rebellion_suppressed"),
		(assign,'$shugo_rebel_flag',0),
		(call_script,'script_succeed_quest','qst_bushi_suppress_rebels'),
		]
	),
# Scout
(1.0, 0, 0.0, 
	[
		(check_quest_active,'qst_bushi_scout_enemy_patrol'),
		(neg|check_quest_succeeded,'qst_bushi_scout_enemy_patrol'),
		],
	[
		(assign,':break',false),
		(quest_get_slot,':target_template','qst_bushi_scout_enemy_patrol',slot_quest_target_party_template),
		(party_get_skill_level,':party_pathfinding_skill','p_main_party',skl_pathfinding),
		(try_begin),
			(is_currently_night),
			(store_add,':max_detection_distance',':party_pathfinding_skill',5),
		(else_try),
			(store_add,':max_detection_distance',':party_pathfinding_skill',5),
		(try_end),
		(try_for_parties,':party_no'),
			(eq,':break',false),
			(party_get_template_id,':template',':party_no'),
			(eq,':template',':target_template'),
			(store_distance_to_party_from_party,':dist','p_main_party',':party_no'),
			(le,':dist',':max_detection_distance'),
			(quest_set_slot,'qst_jb_faction_scout',slot_quest_target_party,':party_no'),
			(assign,':break',true),
			(call_script,'script_succeed_quest','qst_bushi_scout_enemy_patrol'),
		(try_end),
		],
	),
#Visiting kuge quest triggers
#    (0.0, 0, 0.0, [(check_quest_active,"qst_entertain_visiting_kuge")],
#        [
#            (store_current_day,reg2),
#            (assign,reg10,"$kuge_quest_time"),
#            (val_sub,reg10,reg2),
#            (str_store_troop_name,1,"$kuge_quest_giver"),
#            (str_store_item_name,2,"$kuge_quest_item"),
#            (setup_quest_text,"qst_entertain_visiting_kuge")
#        ]),
#    (0.0, 0, 0.0, [(check_quest_active,"qst_entertain_visiting_kuge"),
#                    (store_current_day,reg2),
#                    (gt,reg2,"$kuge_quest_time")],
#        [
#            (fail_quest,"qst_entertain_visiting_kuge")
#        ]),
#Emissary quest triggers
(0.0, 0, 12.0, 
	[
		(check_quest_active,"qst_kuge_emissary"),
		(neg|check_quest_succeeded,'qst_kuge_emissary'),
		],
	[
		(call_script,'script_succeed_quest','qst_kuge_emissary'),
		(display_message,"str_emissary_complete"),
		]),
#Intro bandits behavior
	(0.0, 0, 0.0, [(eq,"$bandit_intro",1)],
         [
            (store_distance_to_party_from_party,reg1,"$intro_bandit_party","p_bandit_camp"),
            (try_begin),
                (gt,reg1,1),
                (party_set_ai_behavior,"$intro_bandit_party",ai_bhvr_travel_to_party),
                (party_set_ai_object,"$intro_bandit_party","p_bandit_camp"),
            (else_try),
                (party_set_ai_behavior,"$intro_bandit_party",ai_bhvr_hold),
            (try_end)]),
# random ronin investigator quests
# qst defeat bandit parties
	(0.0, 0, 0.0,
		[
			(check_quest_active,'qst_ronin_defeat_bandit_parties'),
			(neg|check_quest_concluded,'qst_ronin_defeat_bandit_parties'),
			(quest_get_slot,reg1,'qst_ronin_defeat_bandit_parties',slot_quest_pt_count),
			(quest_get_slot,reg2,'qst_ronin_defeat_bandit_parties',slot_quest_pt_defeated),
			(lt,reg2,reg1),
			],
		[
			(val_mul,reg2,100),
			(val_div,reg2,reg1),
			(set_quest_progression,'qst_ronin_defeat_bandit_parties',reg2),
			],
		),
	(0.1, 0, 0.0,
		[
			(check_quest_active,'qst_ronin_defeat_bandit_parties'),
			(neg|check_quest_succeeded,'qst_ronin_defeat_bandit_parties'),
			(quest_get_slot,':num_to_defeat','qst_ronin_defeat_bandit_parties',slot_quest_pt_count),
			(quest_slot_eq,'qst_ronin_defeat_bandit_parties',slot_quest_pt_defeated,':num_to_defeat'),
			],
		[
			(call_script,'script_succeed_quest','qst_ronin_defeat_bandit_parties'),
			(assign,'$g_bandit_spawn_flag',1),
			(display_message,'str_ronin_bandits_defeated'),
			]
		),
# qst_investigate_bandit_activity
	(0.0, 0, 0.0, [(quest_slot_eq,'qst_ronin_investigate_bandit_activity',slot_quest_flags,15)],
		[	
			(quest_set_slot,'qst_ronin_investigate_bandit_activity',slot_quest_flags,16),
			(quest_get_slot,':target_city','qst_ronin_investigate_bandit_activity',slot_quest_party_1),
			(set_spawn_radius,20),
			(spawn_around_party,':target_city','pt_bandit_raiders_b'),
			(assign,':target_party',reg0),
			(party_add_members,':target_party','trp_bandit_leader',1),
			(party_set_ai_behavior,':target_party',ai_bhvr_patrol_party),
			(party_set_ai_object,':target_party',':target_city'),
			(party_set_ai_patrol_radius,':target_party',10),
			(call_script,'script_object_modify_slot',obj_party,':target_party',slot_spawn_att_to_player,op_asn,att_hostile),
			(quest_set_slot,'qst_ronin_investigate_bandit_activity',slot_quest_target_party,':target_party'),
			]
		),
	(0.0, 0, 0.0,
		[
			(check_quest_active,'qst_ronin_investigate_bandit_activity'),
			(quest_slot_eq,'qst_ronin_investigate_bandit_activity',slot_quest_flags,16),
			(quest_get_slot,':target_party','qst_ronin_investigate_bandit_activity',slot_quest_target_party),
			(neg|party_is_active,':target_party'),
			],
		[
			(call_script,'script_object_modify_slot',obj_quest,'qst_ronin_investigate_bandit_activity',slot_quest_flags,op_asn,null),
			(call_script,'script_succeed_quest','qst_ronin_investigate_bandit_activity'),
			]
		),
	
	(0.0, 96, 0.0, 
		[
			(eq,'$g_bandit_spawn_flag',1),
			],
		[
			(assign,'$g_bandit_spawn_flag',0),
			]
		),

# storyline quest bushi triggers
# rebellion
    (1.0, 0, 0.0, [(check_quest_active,'qst_storyline_bushi_rebellion'),
                   (neg|check_quest_succeeded,'qst_storyline_bushi_rebellion'),
                   (store_add,':num_total_parties','$num_rebel_parties','$num_rebel_parties_destroyed'),
                   (store_num_parties_destroyed_by_player,reg2,'pt_ikko_ikki_rebels'),
                   (ge,reg2,':num_total_parties')],
     [(display_message,'str_shugo_rebellion_suppressed'),
      (call_script,'script_succeed_quest','qst_storyline_bushi_rebellion'),
      ]),
# outpost
    (1.0, 0, 0.0, [(check_quest_active,'qst_storyline_bushi_establish_outpost'),
                   (neg|check_quest_succeeded,'qst_storyline_bushi_establish_outpost'),
                   (eq,'$fort_complete',1)],
     [
         (assign,'$fort_complete',0),
         (call_script,'script_succeed_quest','qst_storyline_bushi_establish_outpost'),
         (display_message,'str_storyline_bushi_single_outpost'),
         ]),
# enemy patrols
    (1.0, 0, 0.0, [(check_quest_active,'qst_storyline_bushi_enemy_patrols'),
                   (neg|check_quest_succeeded,'qst_storyline_bushi_enemy_patrols'),
                   (store_num_parties_destroyed_by_player,reg2,'$target_enemy_patrol'),
                   (val_sub,reg2,'$num_enemy_patrols_destroyed'),
                   (ge,reg2,10)],
     [
         (call_script,'script_succeed_quest','qst_storyline_bushi_enemy_patrols'),
         (display_message,'str_storyline_bushi_enemy_patrols'),
         ]),
# enryaku-ji mountain bandits
(0.0, 0, 0.0,
	[
		(check_quest_active,'qst_defeat_mountain_bandits'),
		(neg|check_quest_succeeded,'qst_defeat_mountain_bandits'),
		],
	[
		(quest_get_slot,':count','qst_defeat_mountain_bandits',slot_quest_troop_1_cnt),
		(store_num_parties_destroyed_by_player,reg3,"pt_mountain_bandits"),
		(val_sub,reg3,'$num_mountain_bandits'),
		(assign,reg2,100),
		(val_mul,reg2,reg3),
		(val_div,reg2,':count'),
		(set_quest_progression,"qst_defeat_mountain_bandits",reg2),
		]
	),
(0.1, 0, ti_once, 
	[
		(check_quest_active,'qst_defeat_mountain_bandits'),
		(neg|check_quest_succeeded,'qst_defeat_mountain_bandits'),
		(quest_get_slot,':count','qst_defeat_mountain_bandits',slot_quest_troop_1_cnt),
		(store_num_parties_destroyed_by_player,reg1,"pt_mountain_bandits"),
		(val_sub,reg1,'$num_mountain_bandits'),
		(ge,reg1,':count'),
		],
	[
		(set_quest_progression,"qst_defeat_mountain_bandits",100),
		(call_script,'script_succeed_quest',"qst_defeat_mountain_bandits"),
		(val_sub,"$max_mountain_bandits",6),
		(display_message,"@You have broken the strength of the bandits near En-ryakuji. Return to the temple for your reward."),
		]
	),
########################
#
# MERCHANT QUEST TRIGGERS
#
########################

# qst_merchant_patrol_trade_route
(24.0, 0, 0,
	[
		(check_quest_active,'qst_merchant_patrol_trade_route'),
		(quest_get_slot,':target_party','qst_merchant_patrol_trade_route',slot_quest_target_party),
		(party_is_active,':target_party'),
		(quest_slot_ge,'qst_merchant_patrol_trade_route',slot_quest_time_to_completion,0),
		],
	[
		(call_script,'script_object_modify_slot',obj_quest,'qst_merchant_patrol_trade_route',slot_quest_time_to_completion,op_sub,1)
		# (quest_get_slot,':time_to_completion','qst_merchant_patrol_trade_route',slot_quest_time_to_completion),
		# (val_sub,':time_to_completion',1),
		# (quest_set_slot,'qst_merchant_patrol_trade_route',slot_quest_time_to_completion,':time_to_completion'),
		]
	),
(2.0, 0, 0,
	[
		(check_quest_active,'qst_merchant_patrol_trade_route'),
		(neg|check_quest_succeeded,'qst_merchant_patrol_trade_route'),
		(neg|check_quest_failed,'qst_merchant_patrol_trade_route'),
		],
	[
		(quest_get_slot,':spawn_point','qst_merchant_patrol_trade_route',slot_quest_party_3),
		(set_spawn_radius,0),
		(spawn_around_party,':spawn_point','pt_bandits'),
		(party_set_slot,reg0,slot_spawn_att_to_player,att_hostile),
		]
	),
(1.0, 0, 0,
	[
		(check_quest_active,'qst_merchant_patrol_trade_route'),
		(neg|check_quest_succeeded,'qst_merchant_patrol_trade_route'),
		(neg|check_quest_failed,'qst_merchant_patrol_trade_route'),
		# (quest_get_slot,':target_party','qst_merchant_patrol_trade_route',slot_quest_target_party),
		# (this_or_next|neg|party_is_active,':target_party'),
	    (quest_slot_eq,'qst_merchant_patrol_trade_route',slot_quest_time_to_completion,0),
		],
	[
		(try_begin),
			(quest_get_slot,':target_party','qst_merchant_patrol_trade_route',slot_quest_target_party),
			(neg|party_is_active,':target_party'),
			(call_script,'script_succeed_quest','qst_merchant_patrol_trade_route'),
		(else_try),
			(quest_get_slot,':target_party','qst_merchant_patrol_trade_route',slot_quest_target_party),
			(remove_party,':target_party'),
			(call_script,'script_fail_quest','qst_merchant_patrol_trade_route'),
		(try_end),
		]
	),
# qst_merchant_deliver_shipment
(24.0, 0, 0,
	[
		(check_quest_active,'qst_merchant_deliver_shipment'),
		(quest_slot_ge,'qst_merchant_patrol_trade_route',slot_quest_time_to_completion,0),
		],
	[
		(quest_get_slot,':time_to_completion','qst_merchant_deliver_shipment',slot_quest_time_to_completion),
		(val_sub,':time_to_completion',1),
		(quest_set_slot,'qst_merchant_deliver_shipment',slot_quest_time_to_completion,':time_to_completion'),
		]
	),
(1.0, 0, 0,
	[
		(check_quest_active,'qst_merchant_deliver_shipment'),
		(quest_slot_eq,'qst_merchant_deliver_shipment',slot_quest_time_to_completion,0),
		],
	[
		(call_script,'script_fail_quest','qst_merchant_deliver_shipment'),
		]
	),
]
debug = """


###############################################################################################
############################################################################### DEBUG TRIGGERS
###############################################################################################






#############

#Captivity:

#  (1.0, 0, 0.0, [],
#   [
#       (ge,"$captivity_mode",1),
#       (store_current_hours,reg1),
#       (val_sub,reg1,"$captivity_end_time"),
#       (ge,reg1,0),
#       (display_message,"str_nobleman_reached_destination"),
#       (jump_to_menu,"$captivity_end_menu"),
#    ]),


  (1.0, 0, ti_once, [], [(assign,"$peak_river_pirates",6)]),
  (3.7, 0, 0.0, [#[store0_distance_to_town_from_party,zendar],[lt,reg0,40],
                 (store_num_parties_of_template, reg(2), "pt_river_pirates"),
                 (lt,reg(2),"$peak_river_pirates")],
                    [(set_spawn_radius,1),(spawn_around_party,"p_river_pirate_spawn_point","pt_river_pirates")]),

  (5.7, 0, 0.0, [(store_num_parties_created,reg3,"pt_manhunters"),
                 (lt,reg3,num_max_zendar_manhunters),
                 (store_num_parties_of_template, reg(2), "pt_manhunters"), (lt,reg(2),3)],
                       [(set_spawn_radius,1),(spawn_around_party,"p_zendar","pt_manhunters")]),
#  (5.2, 0, 0.0, [(store_num_parties_created,reg3,"pt_peasant"),(lt,reg3,num_max_zendar_peasants),
#                 (store_num_parties_of_template, reg(2), "pt_peasant"), (lt,reg(2),3)],
#                          [(set_spawn_radius,1),(spawn_around_party,"p_zendar","pt_peasant")]),
  

  (0.0, 0, 0.0, [(neq,"$quest_accepted_zendar_river_pirates",0),(eq,"$quest_complete_zendar_river_pirates",0)],
   [
    (store_num_parties_destroyed_by_player, ":num_river_pirates_destroyed", "pt_river_pirates"),
    (assign, ":percentage", 100),
    (val_mul, ":percentage", ":num_river_pirates_destroyed"),
    (val_div, ":percentage", num_max_river_pirates),
    (set_quest_progression, "qst_hunt_down_river_pirates", ":percentage"),
   ]),
  
  (0.1, 0, ti_once, [(store_num_parties_destroyed_by_player, ":num_river_pirates_destroyed", "pt_river_pirates"),
                     (ge,":num_river_pirates_destroyed",num_max_river_pirates)],
                          [(assign,"$quest_complete_zendar_river_pirates",1),
                           (set_quest_progression, "qst_hunt_down_river_pirates", 100),
                           (assign,"$peak_river_pirates",2)]),
 

#Tax Collectors
# Prisoner Trains
#  (4.1, 0, 0.0, [],
#                     [
#                         (assign, "$pin_faction", "fac_swadians"),
#                         (assign, "$pin_party_template", "pt_swadian_prisoner_train"),
#                         (assign, "$pin_limit", peak_prisoner_trains),
#                         (call_script,"script_cf_spawn_party_at_faction_town_if_below_limit"),
#                         (party_set_ai_behavior,"$pout_party",ai_bhvr_travel_to_party),
#                         (party_set_ai_object,"$pout_party","$pout_town"),
#                    ]),
#
#  (4.1, 0, 0.0, [],
#                     [
#                         (assign, "$pin_faction", "fac_vaegirs"),
#                         (assign, "$pin_party_template", "pt_vaegir_prisoner_train"),
#                         (assign, "$pin_limit", peak_prisoner_trains),
#                         (call_script,"script_cf_spawn_party_at_faction_town_if_below_limit"),
#                         (party_set_ai_behavior,"$pout_party",ai_bhvr_travel_to_party),
#                         (party_set_ai_object,"$pout_party","$pout_town"),
#                    ]),

  (2.0, 0, 0, [(store_random_party_of_template, reg(2), "pt_prisoner_train_party"),
               (party_is_in_any_town,reg(2)),
               ],
              [(store_faction_of_party, ":faction_no", reg(2)),
               (call_script,"script_cf_select_random_center_with_faction", ":faction_no"),
               (party_set_ai_behavior,reg(2),ai_bhvr_travel_to_party),
               (party_set_ai_object,reg(2),reg0),
               (party_set_flags, reg(2), pf_default_behavior, 0),
            ]),

##Caravans
#  (4.2, 0, 0.0, [],
#                     [
#                         (assign, "$pin_faction", "fac_swadians"),
#                         (assign, "$pin_party_template", "pt_swadian_caravan"),
#                         (assign, "$pin_limit", peak_kingdom_caravans),
#                         (call_script,"script_cf_spawn_party_at_faction_town_if_below_limit"),
#                         (party_set_ai_behavior,"$pout_party",ai_bhvr_travel_to_party),
#                         (party_set_ai_object,"$pout_party","$pout_town"),
#                    ]),

#  (4.2, 0, 0.0, [],
#                     [
#                         (assign, "$pin_faction", "fac_vaegirs"),
#                         (assign, "$pin_party_template", "pt_vaegir_caravan"),
#                         (assign, "$pin_limit", peak_kingdom_caravans),
#                         (call_script,"script_cf_spawn_party_at_faction_town_if_below_limit"),
#                         (party_set_ai_behavior,"$pout_party",ai_bhvr_travel_to_party),
#                         (party_set_ai_object,"$pout_party","$pout_town"),
#                    ]),

##  (2.0, 0, 0, [(store_random_party_of_template, reg(2), "pt_kingdom_caravan_party"),
##               (party_is_in_any_town,reg(2)),
##               ],
##              [(store_faction_of_party, ":faction_no", reg(2)),
##               (call_script,"script_cf_select_random_town_with_faction", ":faction_no"),
##               (party_set_ai_behavior,reg(2),ai_bhvr_travel_to_party),
##               (party_set_ai_object,reg(2),reg0),
##               (party_set_flags, reg(2), pf_default_behavior, 0),
##            ]),
  
  (4.0, 0, 0.0, [(eq, "$caravan_escort_state", 1), #cancel caravan_escort_state if caravan leaves the destination
                 (get_party_ai_object,reg1,"$caravan_escort_party_id"),
                 (neq,reg1,"$caravan_escort_destination_town"),
                ],
                     [(assign,"$caravan_escort_state",0),
#                      (add_xp_as_reward,100),
                      ]),

#Messengers
#  (4.2, 0, 0.0, [],
#   [(assign, "$pin_faction", "fac_swadians"),
#    (assign, "$pin_party_template", "pt_swadian_messenger"),
#    (assign, "$pin_limit", peak_kingdom_messengers),
#    (call_script,"script_cf_spawn_party_at_faction_town_if_below_limit"),
#    (party_set_ai_behavior,"$pout_party",ai_bhvr_travel_to_party),
#    (party_set_ai_object,"$pout_party","$pout_town"),
#    ]),

#  (4.2, 0, 0.0, [],
#   [(assign, "$pin_faction", "fac_vaegirs"),
#    (assign, "$pin_party_template", "pt_vaegir_messenger"),
#    (assign, "$pin_limit", peak_kingdom_caravans),
#    (call_script,"script_cf_spawn_party_at_faction_town_if_below_limit"),
#    (party_set_ai_behavior,"$pout_party",ai_bhvr_travel_to_party),
#    (party_set_ai_object,"$pout_party","$pout_town"),
#    ]),

  (1.5, 0, 0, [(store_random_party_of_template, reg(2), "pt_messenger_party"),
               (party_is_in_any_town,reg(2)),
               ],
   [(store_faction_of_party, ":faction_no", reg(2)),
    (call_script,"script_cf_select_random_center_with_faction", ":faction_no"),
    (party_set_ai_behavior,reg(2),ai_bhvr_travel_to_party),
    (party_set_ai_object,reg(2),reg0),
    (party_set_flags, reg(2), pf_default_behavior, 0),
    ]),
  
  

  (24.0, 0.0, 0.0,
   [
       (ge,"$mountain_bandits_paid",1)
    ],
   [
       (val_sub,"$mountain_bandits_paid",1),
       (eq,"$mountain_bandits_paid",0),
       (set_relation,"fac_mountain_bandits", "fac_player_faction", "$mountain_bandits_relation_original"),
    ]
   ),

  (24.0, 0.0, 0.0,
   [
       (ge,"$forest_bandits_paid",1)
    ],
   [
       (val_sub,"$forest_bandits_paid",1),
       (eq,"$forest_bandits_paid",0),
       (set_relation,"fac_forest_bandits", "fac_player_faction", "$forest_bandits_relation_original"),
    ]
   ),
#Deserters

#  (10.2, 0, 0.0, [],
#                     [
#                         (assign, "$pin_faction", "fac_swadians"),
#                         (assign, "$pin_party_template", "pt_swadian_deserters"),
#                         (assign, "$pin_limit", 4),
#                         (call_script,"script_cf_spawn_party_at_faction_town_if_below_limit"),
#                    ]),
  
#  (10.2, 0, 0.0, [],
#                     [
#                         (assign, "$pin_faction", "fac_vaegirs"),
#                         (assign, "$pin_party_template", "pt_vaegir_deserters"),
#                         (assign, "$pin_limit", 4),
#                         (call_script,"script_cf_spawn_party_at_faction_town_if_below_limit"),
#                    ]),
  
#  (24.0, 0.0, 0.0,
#   [
#       (ge,"$deserters_paid",1)
#    ],
#   [
#       (val_sub,"$deserters_paid",1),
#       (eq,"$deserters_paid",0),
#       (set_relation,"fac_deserters", "fac_player_faction", "$deserter_relation_original"),
#    ]
#   ),


#Kingdom Parties
  (1.0, 0, 0.0, [],
   [(try_for_range, ":cur_kingdom", kingdoms_begin, kingdoms_end),
##      (try_begin),
##        (store_random_in_range, ":random_no", 0, 100),
##        (lt, ":random_no", 10),
##        (call_script, "script_create_kingdom_party_if_below_limit", ":cur_kingdom", spt_forager),
##      (try_end),
##      (try_begin),
##        (store_random_in_range, ":random_no", 0, 100),
##        (lt, ":random_no", 10),
##        (call_script, "script_create_kingdom_party_if_below_limit", ":cur_kingdom", spt_scout),
##      (try_end),
##      (try_begin),
##        (store_random_in_range, ":random_no", 0, 100),
##        (lt, ":random_no", 10),
##        (call_script, "script_create_kingdom_party_if_below_limit", ":cur_kingdom", spt_patrol),
##      (try_end),
##      (try_begin),
##        (store_random_in_range, ":random_no", 0, 100),
##        (lt, ":random_no", 10),
##        (call_script, "script_create_kingdom_party_if_below_limit", ":cur_kingdom", spt_messenger),
##      (try_end),
      (try_begin),
        (store_random_in_range, ":random_no", 0, 100),
        (lt, ":random_no", 10),
        (call_script, "script_create_kingdom_party_if_below_limit", ":cur_kingdom", spt_kingdom_caravan),
      (try_end),
##      (try_begin),
##        (store_random_in_range, ":random_no", 0, 100),
##        (lt, ":random_no", 10),
##        (call_script, "script_create_kingdom_party_if_below_limit", ":cur_kingdom", spt_prisoner_train),
##      (try_end),
##      (try_begin),
##        (store_random_in_range, ":random_no", 0, 100),
##        (lt, ":random_no", 10),
##        (call_script, "script_create_kingdom_party_if_below_limit", ":cur_kingdom", spt_deserter),
##      (try_end),
    (try_end),
    ]),


#########################################################################
# Random MERCHANT quest triggers
####################################  
 # Apply interest to merchants guild debt  1% per week
  (24.0 * 7, 0.0, 0.0,
   [],
   [
       (val_mul,"$debt_to_merchants_guild",101),
       (val_div,"$debt_to_merchants_guild",100)
    ]
   ),
# Deliver food to tavern  
  (24.0, 0, 1.0,
   [
       (eq,"$deliver_wine_quest_active",1),
       (store_current_day,reg3),
       (lt,"$deliver_wine_quest_end_day",reg3),
     ],
   [
       (fail_quest,"qst_deliver_wine"),
       (call_script, "script_end_quest","qst_deliver_wine"),
       (assign,"$deliver_wine_quest_active",0),
       (val_add,"$debt_to_merchants_guild","$deliver_wine_quest_debt")
     ]
   ),
# Escort merchant caravan:
  (0.1, 0.0, 0.1, [[eq,"$escort_merchant_caravan_quest_active",1],
                   [eq,"$escort_merchant_caravan_mode",1]], [[party_set_ai_behavior,"$merchant_caravan_id",ai_bhvr_hold],[party_set_flags, "$merchant_caravan_id", pf_default_behavior, 0]]),
  (0.02, 0.0, 0.1, [[eq,"$escort_merchant_caravan_quest_active",1],
                   [eq,"$escort_merchant_caravan_mode",0],
#                   [store_distance_to_party_from_party, reg0,"$merchant_caravan_id"],
#                   [ge,reg0,1]
                   ], [[party_set_ai_behavior,"$merchant_caravan_id",ai_bhvr_escort_party],[party_set_flags, "$merchant_caravan_id", pf_default_behavior, 0],
                                    [party_set_ai_object,"$merchant_caravan_id",0]]),
  (0.02, 0.0, 0.1, [[eq,"$escort_merchant_caravan_quest_active",1],
                   [eq,"$escort_merchant_caravan_mode",0],
                   [store_distance_to_party_from_party, reg0,"$merchant_caravan_id"],
                   [lt,reg0,1]], [
#                   [party_set_ai_behavior,"$merchant_caravan_id",ai_bhvr_hold],[party_set_flags, "$merchant_caravan_id", pf_default_behavior, 0]
                   ]),

  (0.1, 0, 0.0, [[eq,"$escort_merchant_caravan_quest_active",1],
                 [neg|party_is_active,"$merchant_caravan_id"]],
                         [[fail_quest,"qst_escort_merchant_caravan"],
                          [call_script, "script_end_quest","qst_escort_merchant_caravan"],
                           [assign,"$escort_merchant_caravan_quest_active",0]]),

# Troublesome bandits
  (0.3, 0.0, 1.1, [[eq,"$troublesome_bandits_quest_active",1],
                   [store_num_parties_destroyed,reg3,"pt_troublesome_bandits"],
                   [lt,"$troublesome_bandits_eliminated",reg3],
                   (store_num_parties_destroyed_by_player, ":num_quest_bandits_destroyed_by_player", "pt_troublesome_bandits"),
                   [eq,":num_quest_bandits_destroyed_by_player","$troublesome_bandits_eliminated_by_player"],
                   ], [[display_message,"str_bandits_eliminated_by_another"],
                       [fail_quest,"qst_troublesome_bandits"],
                       [call_script, "script_end_quest","qst_troublesome_bandits"],
                       [assign,"$troublesome_bandits_quest_active",0]]),

  (0.3, 0.0, 1.1, [[eq,"$troublesome_bandits_quest_active",1],
                   [store_num_parties_destroyed,reg3,"pt_troublesome_bandits"],
                   [lt,"$troublesome_bandits_eliminated",reg3],
                   (store_num_parties_destroyed_by_player, ":num_quest_bandits_destroyed_by_player", "pt_troublesome_bandits"),
                   [neq,":num_quest_bandits_destroyed_by_player","$troublesome_bandits_eliminated_by_player"],
                   ], [[succeed_quest,"qst_troublesome_bandits"],
                       [assign,"$troublesome_bandits_quest_active",0],
                       [assign,"$troublesome_bandits_quest_succeeded",1]]),
# Kidnapped girl:
  (0, 0, 0,
   [
       (entering_town,"$kidnapped_girl_quest_giver_town"),
       (main_party_has_troop,"trp_kidnapped_girl")
       ],
   [
       (start_map_conversation,"trp_kidnapped_girl"),
       (assign,"$talking_with_kidnapped_girl",1),
       (assign,"$kidnapped_girl_brought_back",1)
       ]
   ),
  
  (2.0, 0, 0,
   [
      (check_quest_active,"qst_kidnapped_girl"),
      (main_party_has_troop,"trp_kidnapped_girl"),
      (eq,"$kidnapped_girl_bandits_attacked",1),
      (eq,"$kidnapped_girl_joined",0),
    ],
   [
      (assign,"$kidnapped_girl_joined",1),
    ]
   ),"""